<?php
/**
 * Twitter Bootstrap templater.
 */

/**
 * 
 */
require_once('Template.php');

/**
 * Description of BootStrapTemplate
 *
 * @author anthony
 */
class BootStrapTemplate extends Template {
    protected $hasSearchBox = false;
    /**
     * Main Content Feed Generator
     * @param string $feed_title
     * @param string $feed_url
     * @param array $feed_items
     * @param string $rss_html
     * @return type 
     */
    public function maincontent_feed($feed_title, $feed_url, array $feed_items, $rss_html = false) {
	if($rss_html === false) {
	    $rss_output = '&nbsp;';
	} else {
	    $rss_output = $rss_html;
	}
	$html = <<<EOHEAD
	
		<hr />
		<div class="row-fluid">
		    <div class="span11" style="text-align: center;">
			<h2>{$feed_title}</h2>
		    </div>
		    <div class="span1" style="text-align: right;">
			{$rss_output}
		    </div>
		</div>
		<hr />
	
EOHEAD;
	foreach($feed_items as $key => $item) {
	    $html .= <<<EOHERE
		
		<div class="row-fluid">
		<div class="span3">
		    <div style="text-align: center; width: 100%;">
			{$item['icon']}
			Added: {$item['date']}
		    </div>
		</div>
		<div class="span9">
		    <h2><a href="{$item['uri']}">{$item['title']}</a></h2>
		    <p>
			{$item['description']}
		    </p>
		    <p><a class="btn" href="{$item['uri']}">View &raquo;</a></p>
		</div>
		</div><hr />
EOHERE;
	}
	return $html;
    }
    
    /**
     * Main Content Feed Generator
     * @param string $feed_title
     * @param string $feed_url
     * @param array $feed_items
     * @param string $rss_html
     * @return type 
     */
    public function maincontent_userlist($feed_title, $feed_url, array $feed_items, $rss_html = false) {
	if($rss_html === false) {
	    $rss_output = '&nbsp;';
	} else {
	    $rss_output = $rss_html;
	}
	$html = <<<EOHEAD
	
		<hr />
		<div class="row-fluid">
		    <div class="span11" style="text-align: center;">
			<h2>{$feed_title}</h2>
		    </div>
		    <div class="span1" style="text-align: right;">
			{$rss_output}
		    </div>
		</div>
		<hr />
	
EOHEAD;
	foreach($feed_items as $key => $item) {
	    $html .= <<<EOHERE
		
		<div class="row-fluid">
		<div class="span3">
		    <div style="text-align: center; width: 100%;">
			{$item['icon']}
		    </div>
		</div>
		<div class="span9">
		    <h2><a href="{$item['uri']}">{$item['title']}</a></h2>
		    <p>
			{$item['description']}
		    </p>
		    <p><a class="btn" href="{$item['uri']}">View &raquo;</a></p>
		</div>
		</div><hr />
EOHERE;
	}
	return $html;
    }
    
    /**
     * Create a twitter display item in our colours
     * @param string $username
     * @return type 
     */
    public function maketwitterFeed($username) {
	return parent::maketwitterFeed($username, '#c9c9c9', '#000000', '#ffffff', '#595959', '#000000');
    }

    /**
     * Create a sidebar item with a title
     * @param string $title
     * @param string $html
     * @return type 
     */
    public function createSidebarItem($title, $html) {
	return <<<EOH
	    <li class="nav-header">{$title}</li>
	      {$html}
EOH;
    }
    
    /**
     * Create a drop down item for the top right user box
     * @param boolean $divider - Is this a divider?
     * @param string $url - Not needed for divider
     * @param string $html - Not needed for divider
     * @return string
     * @throws Exception 
     */
    public function create_dropdownItem($divider = true, $url = false, $html = false) {
	if($divider === true) {
	    return '<li class="divider"></li>';
	} elseif($divider === false && is_string($url) && is_string($html)) {
	    return <<<EOH
		<li><a href="{$url}">{$html}</a></li>
EOH;
	} elseif(is_string($url) === false || is_string($html) === false) {
	    throw new Exception("Non-divider requested. Please specify a url and html for the link");
	} else {
	    throw new Exception("Boolean expected for if this is a divider! First argument to `".__CLASS__.'->'.__METHOD__.'` is invalid');
	}
    }

    /**
     * Make Project Unit. This creates a hero unit for content to be displayed on. It is used to render the project view
     * @param string $title
     * @param string $icon
     * @param string $htmlblock1
     * @param string $htmlblock2
     * @return type 
     */
    public function makeprojectunit($title, $icon, $htmlblock1, $htmlblock2) {
	if($icon !== false) {
	    $image = '<img src="'.$icon.'" style="width: 40px; height: 40px; border: 0px solid none;" />';
	} else {
	    $image = '';
	}
	return <<<EOH
	    <div class="hero-unit" style="padding-top: 2px !IMPORTANT; padding-bottom: 2px !IMPORTANT;">
		<hr /><div style="vertical-align: middle;">
		    {$image}
		    <span style="font-size: 20px;">{$title}</span>
		</div><hr />
		<div class="row-fluid">
		    <div class="span9">{$htmlblock1}</div>
		    <div class="span3">{$htmlblock2}</div>
		</div>
	    </div>
EOH;
    }

    /**
     * Generate HTML for the lead maintainer box
     * @global Stash $UserStash
     * @param int $lead_uid
     * @param string $maintainers
     * @return type 
     */
    public function makeleadmaintainerbox($lead_uid, $maintainers) {
	global $UserStash;
	$User =& $UserStash->get($lead_uid);
	
	$icon = $User->get_profile_icon();
	if($icon !== false) {
	    $image = '<img src="'.$icon.'" style="width: 40px; height: 40px; border: 0px solid none;" /><br />';
	}
	$mantrs = "";
	/*foreach($maintainers as $key => $value) {
	    $mantrs .= $UserStash->get($value)->get_username();
	}*/
	return <<<EOH
	    <div class="span12">
		<div class="row-fluid">
		    <!--<div class="span8" style="text-align: right;"><b>Lead Maintainer&nbsp;:</b><br /></div>-->
		    <div class="span12" style="text-align: center;"><b>Lead Maintainer</b><br /><a href="{$User->get_profile_url()}">{$image}{$User->get_username()}</a></div>
		</div>
		<!--<div class="row-fluid">
		    <div class="span8" style="text-align: right;"><b>Maintainers&nbsp;:</b><br /></div>
		    <div class="span4">{$mantrs}</div>
		</div>-->
	    </div>
EOH;
    }

    /**
     * Generate the dependency html
     * @global Stash $ProjectStash
     * @param array $requirement_bool
     * @param array $requirement_array
     * @return type
     * @throws RuntimeException 
     */
    public function makedependencyrow($requirement_bool, $requirement_array) {
	global $ProjectStash;
	/*echo '<pre>'.print_r($requirement_array, true).'</pre>';
	die();
	if(isset($requirement_array['dependency']) === false || isset($requirement_array['comment']) === false || isset($requirement_array['emde_depd_pid']) === false) {
	    throw new RuntimeException("That is not a dependency array!");
	}*/
	// $requirement_bool can be used to display in a different format if you wish
	if($requirement_array['dependency'] === null) {
	    if($requirement_array['emde_depd_pid'] === null) {
		throw new RuntimeException("Invalid Depdency Row!");
	    } else {
		// Valid - Resolving Internal Dependency
		$IntDepd = $ProjectStash->get(((int)$requirement_array['emde_depd_pid']));
		$A_URL = EclipseMDE::aliasLink($IntDepd->getAlias());
		$Name = '<a href="'.$A_URL.'">'.$IntDepd->getName().'</a>';
	    }
	} else {
	    if($requirement_array['emde_depd_pid'] === null) {
		// Valid - Resolving External Dependency
		$Name = $requirement_array['dependency'];
	    } else {
		throw new RuntimeException("Invalid Depdency Row!");
	    }
	}
	
	return <<<EODR
	<li>
	    {$Name}<br /><i>{$requirement_array['comment']}</i>
	</li>
EODR;
    }

    /**
     * Create comment html
     * TODO: Functionality
     * @param type $commentObject
     * @throws RuntimeException 
     */
    public function makeusercomment($commentObject) {
	if(get_class($commentObject) == 'Class' || is_subclass_of($commentObject, 'Class')) {
	    
	} else {
	    throw new RuntimeException(''.__CLASS__.'->'.__METHOD__.' expects argument 1 to be an instance (or instance of a subclass) of `Comment`. Received `'.get_class($commentObject).'`.');
	}
    }

    /**
     * Return the image for the current rating of the project
     * @param double $averageRating
     * @return type 
     */
    public function getRatingImage($averageRating) {
	$img_open = '<img ';
	$img_close = ' alt="" id="ratingImg" />';
	$img_style = ' style="width: 120px; height: 30px; border: 0px solid none;" ';
	if($averageRating >= 4.75) {
	    $img_link = '/img/rated/50.png';
	} elseif($averageRating >= 4.25) {
	    $img_link = '/img/rated/45.png';
	} elseif($averageRating >= 3.75) {
	    $img_link = '/img/rated/40.png';
	} elseif($averageRating >= 3.25) {
	    $img_link = '/img/rated/35.png';
	} elseif($averageRating >= 2.75) {
	    $img_link = '/img/rated/30.png';
	} elseif($averageRating >= 2.25) {
	    $img_link = '/img/rated/25.png';
	} elseif($averageRating >= 1.75) {
	    $img_link = '/img/rated/20.png';
	} elseif($averageRating >= 1.25) {
	    $img_link = '/img/rated/15.png';
	} elseif($averageRating >= 0.75) {
	    $img_link = '/img/rated/10.png';
	} elseif($averageRating >= 0.25) {
	    $img_link = '/img/rated/05.png';
	} else {
	    $img_link = '/img/rated/00.png';
	}
	return ($img_open.' src="'.$img_link.'"'.$img_style.$img_close);
    }

    /**
     * Generate the html for the rating button
     * @param type $alias
     * @return type 
     */
    public function AJAX_ratingButton($alias) {
	return <<<EOR
	    <div>
		<b>Rate: </b>&nbsp;
		    <select name="{$alias}" style="width: 50px;" onchange="ratingButtonClick(this, '{$alias}');">
			<option value="0">0</option>
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="4">4</option>
			<option value="5">5</option>
		    </select>
	    </div>
EOR;
    }
    
    /**
     * Create the div at the top (little edit button with "this is your project")
     * @param string $text
     * @param string $base_url
     * @param string $alias
     * @return type 
     */
    public function makeEditDiv($text, $base_url, $alias) {
	return $this->makeTopDiv('<a href="'.$base_url.$alias.'" style="border-bottom: 1px dotted #989898;" >'.$text.' <img src="/img/set.gif" style="width: 16px; height: 16px; border-style: 0px solid none;" alt="'.$text.'" /></a>', true);
    }
    
    /**
     * Create the div at the top 
     * @param string $text
     * @param string $base_url
     * @param string $alias
     * @return type 
     */
    public function makeTopDiv($replace_html, $override_defaults = false) {
        if($override_defaults === false) {
            $replace_html = '<span style="border-bottom: 1px dotted #989898;">'.$replace_html.'</span>';
        }            
	return <<<EOR
	    <div class="row-fluid">
		<div class="span12" style="text-align: right;">
		    {$replace_html}
		</div>
	    </div>
EOR;
    }

    /**
     * Create HTML for a search box for projects 
     */
    public function project_searchbox($admin = false) {
	if($this->hasSearchBox === false) {
	    global $TemplateEngine;
	    $TemplateEngine->add_postloadScript('<script type="text/javascript" src="/js/ProjectSearchBox.js"></script>');
	    $TemplateEngine->add_postloadScript("<script type=\"text/javascript\">setTimeout('ProjectSearchBox.init()', '1250');</script>");
	    $this->hasSearchBox = true;
	    if($admin === true) {
		$extra_post = '<input type="hidden" name="adm_srch" id="adm_srch" value="adm_srch" />';
	    } else {
		$extra_post = '';
	    }
	    $tooltip_prjKW = $this->create_tooltip('/info/tooltip/projectkeywords');
	    return <<<EOHTML
		<iframe src="" class="hiddenElement" id="search_iframe" name="search_iframe"></iframe>
		<div class="row-fluid" id="search_top_container">
		    <div class="span3"></div>
		    <div class="span6" style="text-align: center;" id="search_inputs_div">
			<form action="/edjx/search/project" method="POST" target="search_iframe" onsubmit="ProjectSearchBox.search();">
			<hr /><h2>Search for a Project</h2><hr />
			<div class="row-fluid">
			    <div class="span4">
				<b>Project Keywords {$tooltip_prjKW}</b> :&nbsp;
			    </div>
			    <div class="span5">
				<input type="text" style="width: 100%;" name="search_project_name" id="search_project_name" />{$extra_post}
				<img src="/img/spin.gif" alt="Please Wait" class="hiddenElement" style="width: 32px; height: 32px;" id="search_waiter" name="search_waiter" />
			    </div>
			    <div class="span3">
				<input type="submit" value="Search" id="search_project_submitbtn" />
			    </div>
			</div>
			</form>
		    </div>
		    <div class="span3"></div>
		</div>
		<div class="row-fluid" id="search_btn_container">
		    <div class="span3"></div>
		    <div class="span6" style="text-align: center;" id="search_results_div">
			
		    </div>
		    <div class="span3"></div>
		</div>
EOHTML;
	} else {
	    throw new LogicException("The javascript uses ID numbers, you cannot have multiple project searchboxes on a page.");
	}
    }
    
    public function user_searchbox($admin = false) {
        if($this->hasSearchBox === false) {
	    global $TemplateEngine;
	    $TemplateEngine->add_postloadScript('<script type="text/javascript" src="/js/UserSearchBox.js"></script>');
	    $TemplateEngine->add_postloadScript("<script type=\"text/javascript\">setTimeout('UserSearchBox.init()', '1250');</script>");
	    $this->hasSearchBox = true;
	    if($admin === true) {
		$extra_post = '<input type="hidden" name="adm_srch" id="adm_srch" value="adm_srch" />';
	    } else {
		$extra_post = '';
	    }
	    
	    return <<<EOHTML
		<iframe src="" class="hiddenElement" id="search_iframe" name="search_iframe"></iframe>
		<div class="row-fluid" id="search_top_container">
		    <div class="span3"></div>
		    <div class="span6" style="text-align: center;" id="search_inputs_div">
			<form action="/edjx/search/user" method="POST" target="search_iframe" onsubmit="UserSearchBox.search();">
			<hr /><h2>Search for a User</h2><hr />
			<div class="row-fluid">
			    <div class="span4">
				<b>User</b> :&nbsp;
			    </div>
			    <div class="span5">
				<input type="text" style="width: 100%;" name="search_user_name" id="search_user_name" />{$extra_post}
				<img src="/img/spin.gif" alt="Please Wait" class="hiddenElement" style="width: 32px; height: 32px;" id="search_waiter" name="search_waiter" />
			    </div>
			    <div class="span3">
				<input type="submit" value="Search" id="search_user_submitbtn" />
			    </div>
			</div>
			</form>
		    </div>
		    <div class="span3"></div>
		</div>
		<div class="row-fluid" id="search_btn_container">
		    <div class="span3"></div>
		    <div class="span6" style="text-align: center;" id="search_results_div">
			
		    </div>
		    <div class="span3"></div>
		</div>
EOHTML;
	} else {
	    throw new LogicException("The javascript uses ID numbers, you cannot have multiple project searchboxes on a page.");
        }
    }
}

?>
